package com.ming.controller;

import com.ming.dao.CustomerMapper;
import com.ming.dao.RecipientMapper;
import com.ming.dao.UserMapper;
import com.ming.pojo.Customer;
import com.ming.pojo.Recipient;
import com.ming.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Xu
 * Created by Intellij IDEA.
 * @create 2021/4/12 19:54
 */
@Controller
public class LoginController {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RecipientMapper recipientMapper;
    @Autowired
    CustomerMapper customerMapper;

    @PostMapping("/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password, Model model,HttpSession session){
        User user = userMapper.queryUserByUsernameAndPassword(username, password);
        if (user==null){
            //登录失败！存放错误信息
            model.addAttribute("msg","用户名密码错误");
            return "index";
        }else {
            //登录成功！将用户信息放入session
            session.setAttribute("loginUser",username);
            //获取权限，根据权限确定身份
            if (user.getAuthority() == 0){
                //管理员
                //登录成功！防止表单重复提交，我们重定向
                return "redirect:/main.html";
            } else if (user.getAuthority() == 1) {
                //员工
                session.setAttribute("staffName",username);
                return "redirect:/main1.html";

            } else if (user.getAuthority() == 2) {
                //用户
                session.setAttribute("userName",username);
                return "redirect:/main2.html";
            }
            return "index";
        }
    }

    @PostMapping("/user/register")
    public String register(@RequestParam("username") String username,@RequestParam("password") String password,
                           @RequestParam("rePassword") String rePassword, Model model,HttpSession session){
        if(userMapper.queryUserByUsername(username) != null){
            model.addAttribute("msg","用户名已存在！");
            return "register";
        } else if (!password.equals(rePassword)){
            model.addAttribute("msg","密码不一致！");
            return "register";
        } else {
            Integer rid = recipientMapper.queryRecipientName(username);
            Integer cid = customerMapper.queryCustomerName(username);
            if(rid == null){
                Integer mId = recipientMapper.getMId();
                recipientMapper.addRecipient(new Recipient(mId,username,null,null,null));
            }
            if(cid == null){
                Integer mId = customerMapper.getMId();
                customerMapper.addCustomer(new Customer(mId,username,null,null,null));
            }
            userMapper.saveUser(new User(0,username,password,2));
            session.setAttribute("loginUser",username);
            return "redirect:/index.html";

        }
    }

    @RequestMapping("/user/loginout")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/index.html";
    }

}
